package cn.dugcrs.org.controller;

import cn.dugcrs.basic.anno.PreAuthorize;
import cn.dugcrs.basic.utils.AjaxResult;
import cn.dugcrs.basic.utils.LoginContext;
import cn.dugcrs.basic.utils.QueryResult;
import cn.dugcrs.org.domain.Employee;
import cn.dugcrs.org.query.EmployeeQuery;
import cn.dugcrs.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /*
    * @Title: findEmployeeById
    * @Description: 查询单条数据
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/22 13:13
    * @Parameters: * @param id
    * @Return cn.dugcrs.org.domain.Employee
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee findEmployeeById(@ApiParam(name="ID属性",required = true)@PathVariable("id") Long id){
        Employee employeeById = employeeService.loadById(id);
        return employeeById;
    }


    /**
    * @Title: findDrawers
    * @Description: 抽屉查询
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/27 17:59
    * @Parameters: * @param id
    * @Return java.util.List<cn.dugcrs.org.domain.Employee>
    */
    @GetMapping("/drawer/{id}")
    @ApiOperation(value = "通过ID查询" )
    public List<Employee> findDrawers(@ApiParam(name="ID属性",required = true)@PathVariable("id") Long id){
        List<Employee> drawers = employeeService.findDrawers(id);
        return drawers;
    }


    /*
    * @Title: findEmployees
    * @Description: 查询符合高级查询条件及分页的全部数据
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/22 13:14
    * @Parameters: * @param employeeQuery
    * @Return cn.dugcrs.basic.utils.QueryResult
    */
    @PostMapping
    public QueryResult findEmployees(@RequestBody EmployeeQuery employeeQuery, HttpServletRequest request){
        System.out.println(LoginContext.getCurrentLogininfo(request));
        System.out.println("=======");
        QueryResult employees = employeeService.queryPage(employeeQuery);
        return employees;
    }

    /**
    * @Title: findEmps
    * @Description: 查询所有
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/24 14:34
    * @Parameters: * @param
    * @Return java.util.List<cn.dugcrs.org.domain.Employee>
    */
    @GetMapping
    public List<Employee> findEmps(){
        List<Employee> emps = employeeService.loadAll();
        return emps;
    }

    /*
    * @Title: findEmployees
    * @Description: 根据id删除
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/22 13:20
    * @Parameters: * @param id
    * @Return cn.dugcrs.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除员工",value = "employee:delete")
    public AjaxResult delEmployeeById(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.success();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /*
    * @Title: addOrUpd
    * @Description: 添加或修改
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/4/22 13:55
    * @Parameters: * @param employee
    * @Return cn.dugcrs.basic.utils.AjaxResult
    */
    @PutMapping
    public AjaxResult addOrUpd(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.success();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PatchMapping
    public AjaxResult batchDelDept(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.success();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


}
